/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *SdProjectStakeholderController
 * SD项目相关方
 *
 * 修改纪录
 * 2021-12-08 版本：1.0 wangxinhua 创建。
 * @version 版本：1.0
 * @author 作者：wangxinhua
 * 创建日期：2021-12-08
 */

package com.topscomm.gpm.controller;

import java.util.Collections;
import java.util.Map;

import com.topscomm.gpm.controller.auto.GpmProjectStakeholderControllerAuto;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.gpm.service.impl.GpmProjectStakeholderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: wangxinhua
 * @date: 2021-12-08
 * @description:SD项目相关方Controller
 * @modify:
 */
@RestController
@RequestMapping("sd/projectStakeholder")
public class GpmProjectStakeholderController extends GpmProjectStakeholderControllerAuto implements IGpmProjectStakeholderController {
	@Autowired
	public GpmProjectStakeholderService gpmProjectStakeholderService;
	@Autowired
	public GpmProjectService gpmProjectService;

	/**
	 * @param paraMap
	 * @return
	 * @author: liuchenguang
	 * @date: 2021-08-19
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value)) {
				if (value.matches("^[0-9]*$") || value.matches(".*\\d+.*")) {
					sql.appendSingle(" and usercode like '{0}%'", value);
				} else {
					sql.appendSingle(" and username like '%{0}%'", value);
				}
			}
		}
		if (paraMap.containsKey("qry_stakeholdercategory")) {
			String value = paraMap.get("qry_stakeholdercategory").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and stakeholdercategory = '{0}' ", value);
			}
		}
		if (paraMap.containsKey("qry_stakeholderimpact")) {
			String value = paraMap.get("qry_stakeholderimpact").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and stakeholderimpact = '{0}' ", value);
			}
		}
		if (paraMap.containsKey("qry_managementstrategy")) {
			String value = paraMap.get("qry_managementstrategy").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and managementstrategy = '{0}' ", value);
			}
		}
		if (paraMap.containsKey("qry_projectcode")) {
			String value = paraMap.get("qry_projectcode").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and projectcode = '{0}' ", value);
			}
		} else if (paraMap.containsKey("qry_projectid")) {
			// 根据项目id查询
			String projectId = ConvertUtil.convertToString(paraMap.get("qry_projectid"));
			if (!StringUtil.isEmpty(projectId)) {
				Map<String, Object> projectCodeMap = gpmProjectService
						.queryFieldsById(projectId, Collections.singletonList("projectcode"));
				String projectCode = ConvertUtil.convertToString(projectCodeMap.get("projectcode"));
				if (!StringUtil.isEmpty(projectId)) {
					sql.appendSingle(" and projectcode = '{0}' ", projectCode);
				}
			}
		}
		if (paraMap.containsKey("qry_enabled")) {
			String value = paraMap.get("qry_enabled").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and enabled = '{0}' ", value);
			}
		}
		return sql.toString();
	}

	/**
	 * @param paraMap
	 * @return
	 * @author: liuchenguang
	 * @date: 2021-08-25
	 * @description:重写refList，去重重复人员
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(
			@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			String sqlWhere = this.spellListSql(paraMap);
			return gpmProjectStakeholderService.listRevokeRepeat(paraMap, sqlWhere);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @param paraMap
	 * @return ResponseResult<Object>
	 * @author: liuchenguang
	 * @date: 2021-08-11
	 * @description:重写获取相关方列表
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(
			@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			String sqlWhere = this.spellListSql(paraMap);
			return gpmProjectStakeholderService.listRevokeSpace(paraMap, sqlWhere);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @param paraMap
	 * @return
	 * @author: liuchenguang
	 * @date: 2021-08-24
	 * @description: 获取工作组分类的内部人员列表
	 * @modify:
	 */
	@GetMapping(value = "/groupList")
	public ResponseResult<Object> groupList(
			@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			return gpmProjectStakeholderService.getGroupList(paraMap);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

